﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace MvvmLight1
{
    public class MyPlayer : MediaElement
    {
        public MyPlayer()
        {
            Loaded += new RoutedEventHandler(MyPlayer_Loaded);
            this.LoadedBehavior = MediaState.Manual;
            MediaOpened += new RoutedEventHandler(MyPlayer_MediaOpened);

            
        }

        void MyPlayer_MediaOpened(object sender, RoutedEventArgs e)
        {
            SetStatus(Status);
        }

        void MyPlayer_Loaded(object sender, RoutedEventArgs e)
        {
            SetStatus(Status);
        }

        public PlayerStatus Status
        {
            get { return (PlayerStatus)GetValue(StatusProperty); }
            set { SetValue(StatusProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Status.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.Register("Status", typeof(PlayerStatus), typeof(MyPlayer), 
            new UIPropertyMetadata(OnStatusPropertyChanged));
        
        private static void OnStatusPropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            var player = (MyPlayer)sender;
            if (!player.IsLoaded) return;

            var status = (PlayerStatus)args.NewValue;
            player.SetStatus(status);
        }

        private void SetStatus(PlayerStatus status)
        {
            switch (status)
            {
                case PlayerStatus.Play:
                    Play();
                    break;
                case PlayerStatus.Stop:
                    Stop();
                    break;
                case PlayerStatus.Pause:
                    Pause();
                    break;
                default:
                    break;
            }
        }

    }

    public enum PlayerStatus
    {
        Play,
        Stop,
        Pause
    }
}
